<template>
  <el-dialog
    :title="!dataForm.centerId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="55%"
    top="2vh"
  >
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="80px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称" />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio v-model="dataForm.sex" label="1">男</el-radio>
        <el-radio v-model="dataForm.sex" label="0">女</el-radio>
      </el-form-item>
      <el-form-item label="头像" prop="profilePicture">
        <el-upload
          ref="my-upload"
          class="upload-demo"
          action="http://localhost:8765/admin/oss/file/upload?module=personal"
          :on-success="handleAvatarSuccess"
          :on-error="handleAvatarError"
          :on-exceed="handleUploadExceed"
          :before-upload="beforeAvatarUpload"
          :file-list="fileList"
          :limit="1"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过2Mb</div>
        </el-upload>
      </el-form-item>
      <el-form-item label="学历" prop="educationBackground">
        <el-select v-model="dataForm.educationBackground" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="qq" prop="qq">
        <el-input v-model="dataForm.qq" placeholder="qq" />
      </el-form-item>
      <el-form-item label="微信" prop="weixin">
        <el-input v-model="dataForm.weixin" placeholder="微信" />
      </el-form-item>
      <el-form-item label="电话号码" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="电话号码" />
      </el-form-item>
      <el-form-item label="个人评价" prop="selfEvaluation">
        <el-input v-model="dataForm.selfEvaluation" type="textarea" autosize placeholder="个人评价" />
      </el-form-item>
      <el-form-item label="兴趣爱好" prop="hobbiesInterests">
        <el-input v-model="dataForm.hobbiesInterests" type="textarea" autosize placeholder="兴趣爱好" />
      </el-form-item>
      <el-form-item label="座右铭" prop="motto">
        <el-input v-model="dataForm.motto" placeholder="座右铭" />
      </el-form-item>
      <el-form-item label="人生观" prop="outlookLife">
        <el-input v-model="dataForm.outlookLife" type="textarea" autosize placeholder="人生观" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>

import reqApi from '@/api/personal'

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        centerId: 0,
        name: '',
        sex: '1',
        profilePicture: '',
        qq: '',
        weixin: '',
        phone: '',
        selfEvaluation: '',
        hobbiesInterests: '',
        educationBackground: '',
        motto: '',
        outlookLife: '',
        modificationTime: ''
      },
      dataRule: {
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '性别不能为空', trigger: 'blur' }
        ],
        profilePicture: [
          { required: true, message: '头像不能为空', trigger: 'blur' }
        ],
        qq: [
          { required: true, message: 'qq不能为空', trigger: 'blur' }
        ],
        weixin: [
          { required: true, message: '微信不能为空', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '电话号码不能为空', trigger: 'blur' }
        ],
        selfEvaluation: [
          { required: true, message: '个人评价不能为空', trigger: 'blur' }
        ],
        hobbiesInterests: [
          { required: true, message: '兴趣爱好不能为空', trigger: 'blur' }
        ],
        educationBackground: [
          { required: true, message: '学历不能为空', trigger: 'blur' }
        ],
        motto: [
          { required: true, message: '座右铭不能为空', trigger: 'blur' }
        ],
        outlookLife: [
          { required: true, message: '人生观不能为空', trigger: 'blur' }
        ],
        modificationTime: [
          { required: true, message: '修改时间不能为空', trigger: 'blur' }
        ]
      },
      options: [{
        value: '初中',
        label: '初中'
      }, {
        value: '高中',
        label: '高中'
      }, {
        value: '专科',
        label: '专科'
      }, {
        value: '本科',
        label: '本科'
      }, {
        value: '研究生',
        label: '研究生'
      }],
      fileList: []
    }
  },
  methods: {
    init(id) {
      this.dataForm.centerId = id || ''
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.centerId) {
          reqApi.getInfo(this.dataForm.centerId).then(({ data }) => {
            if (data) {
              this.dataForm.name = data.personalCenter.name
              this.dataForm.sex = data.personalCenter.sex + ''
              this.fileList = [{ 'url': data.personalCenter.profilePicture }]
              this.dataForm.profilePicture = data.personalCenter.profilePicture
              this.dataForm.qq = data.personalCenter.qq
              this.dataForm.weixin = data.personalCenter.weixin
              this.dataForm.phone = data.personalCenter.phone
              this.dataForm.selfEvaluation = data.personalCenter.selfEvaluation
              this.dataForm.hobbiesInterests = data.personalCenter.hobbiesInterests
              this.dataForm.educationBackground = data.personalCenter.educationBackground
              this.dataForm.motto = data.personalCenter.motto
              this.dataForm.outlookLife = data.personalCenter.outlookLife
              this.dataForm.modificationTime = data.personalCenter.modificationTime
            }
          })
        } else {
          this.fileList = []
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (!this.dataForm.centerId) {
            reqApi.save(this.dataForm).then(({ data }) => {
              if (data) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          } else {
            reqApi.update(this.dataForm).then(({ data }) => {
              if (data) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        }
      })
    },
    // 上传多于一个文件
    handleUploadExceed(files, fileList) {
      this.$message.warning('想要重新上传图片，请先删除已上传的视频')
    },
    // 上传校验
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },

    // 上传成功回调
    handleAvatarSuccess(res, file) {
      console.log(res)
      if (res.success) {
        // console.log(res)
        this.dataForm.profilePicture = res.data.url
        // 强制重新渲染
        // this.$forceUpdate()
      } else {
        this.$message.error('上传失败1')
      }
    },

    // 错误处理
    handleAvatarError() {
      this.$message.error('上传失败2')
    }
  }
}
</script>
